<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Page extends CI_Controller {
    
   	public function __construct()
    {
        parent::__construct();
        $this->load->model('MPage');
        $this->load->model('MBanner');
        $this->load->helper('array');
        $this->load->helper('directory');
        $this->load->model('MSubscriber');
        $visitor['today_visitor'] = $this->MSubscriber->gettodayVisitor(); 
        $visitor['total_visitor'] = $this->MSubscriber->gettotalVisitor();
        $this->load->vars($visitor);
    }
    
	public function index($lang = 'en')
	{
	   if($lang != 'en' && $lang != 'kh')
       {
         redirect('error/http404','refresh');
       }
        $page = $this->MPage->getPagePath('index');
		$data['main'] = 'site/page';
        $data['right'] = "right";//load data for content right 
        $data['page'] = $page;
        $data['lang'] = $lang;
        $data['title'] = $page['title'];
        $data['keyword'] = $page['keyword'];
        $data['description'] = $page['description'];
        $data['pagename'] = 'index';
        if($lang == 'kh')
        {
            $data['title'] = $page['titlekh'];
            $data['keyword'] = $page['keywordkh'];
            $data['description'] = $page['descriptionkh'];
        }
        $data['hmenu'] = $this->MPage->getAllActivePages();   
        $data['banners'] = $this->MBanner->getBannerListByPageId($page['page_id']);   
        $data['partners'] = directory_map('./media/site/footer_slide/web/', FALSE, TRUE); 
        
		$this->load->vars($data);
		$this->load->view('site/layout');
	}
	
	function details($lang, $link = 'index'){
	    
        if($lang != 'en' && $lang != 'kh')
        {
            redirect('error/http404','refresh');
        }
	    $page = $this->MPage->getPagePath($link);
        if(!count($page))
        {
            redirect('error/http404?errorpath='.$link,'refresh');
        }
        $this->MPage->addPopularPage($page['page_id']);
		$data['main'] = 'site/page';
        //feedback
        if($link=='feedback')
            $data['main'] = 'site/feedback_design';
            
        $data['right'] = "right";//load data for content right 
        $data['page'] = $page;
        $data['lang'] = $lang;
        $data['title'] = $page['title'];
        $data['keyword'] = $page['keyword'];
        $data['description'] = $page['description'];
        $data['pagename'] = $link;
        if($lang == 'kh')
        {
            $data['title'] = $page['titlekh'];
            $data['keyword'] = $page['keywordkh'];
            $data['description'] = $page['descriptionkh'];
        }
        $data['hmenu'] = $this->MPage->getAllActivePages();
        if($page['parent_id'] > 0)
        {  
            $data['parent1'] = $this->MPage->getNavigationPageByPageId($page['parent_id']);
            $data['groupmenu'] = $this->MPage->getRelatedArticles($page['parent_id']);
        }
        $data['banners'] = $this->MBanner->getBannerListByPageId($page['page_id']);
		$this->load->vars($data);
		$this->load->view('site/layout');
  	}
    
    function search()
    {
        $data['main'] = 'site/search'; 
        $data['lang'] = 'en';
        $data['title'] = 'Search articles from Chamroeun Microfinance Limited.';
        $data['keyword'] = 'Chamroeun, Microfinance, Limited, Cambodia, Microheranhvathu, Loan, Service, Training, Business, Referral, Social,SKY, GRET, Microinsurance, small loan, developing loan, social emergency loan,';
        $data['description'] = 'Chamroeun microfinance limited';   
        $data['hmenu'] = $this->MPage->getAllActivePages(); 
        if($this->input->post('search'))
        {
            $searches = $this->MPage->search();
            $data['searches'] = $searches;
            if(!count($searches))
                $this->session->set_flashdata('message','No record found!');
        }
        $this->load->vars($data);
        $this->load->view('site/layout');
    }
    
    function sitemap()
    {
        $data['main'] = 'site/sitemap'; 
        $data['lang'] = 'en';
        $data['title'] = 'Search articles from Chamroeun Microfinance Limited.';
        $data['keyword'] = 'Chamroeun, Microfinance, Limited, Cambodia, Microheranhvathu, Loan, Service, Training, Business, Referral, Social,SKY, GRET, Microinsurance, small loan, developing loan, social emergency loan,';
        $data['description'] = 'Chamroeun microfinance limited';   
        $data['hmenu'] = $this->MPage->getAllActivePages();
        $this->load->vars($data);
        $this->load->view('site/layout');
    }
    
    function defaultPage($link)
    {
        //$page = $this->MPage->getPagePath($link);
        //if($page == null || count($page) == 0)
//        {
//           redirect('page/pagenotfound','refresh'); 
//        }
		$data['main'] = 'site/page';
		/*$data['title'] = $page['name'];
		$data['metakeyword']= $page['name'];
		$data['right'] = 'site/fdf';*/
		//$data['page'] = $page;
        $data['lang'] = 'en';
        $data['menus'] = 'menus';//get all active pages from db by lang                 
		$this->load->vars($data);
		$this->load->view('site/layout');    
    }
    
    function pageNotFound()
    {
        $data['hmenu'] = $this->MPage->getAllActivePages();
        $data['main'] = 'site/page-404';          
		$this->load->vars($data);
		$this->load->view('site/layout');
    }
}
